package com.example.front.controller;

import com.example.common.dto.JsonResult;
import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.front.dto.CategoryCreateDto;
import com.example.front.dto.CategoryDto;
import com.example.front.dto.CategorySearchDto;
import com.example.front.serivce.CategoryService;
import com.example.interceptor.UserContext;
import com.example.passport.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "盒子类型管理")
@RestController
@CrossOrigin
@RequestMapping("/front/category")
public class CategoryController {

    @Resource
    CategoryService categoryService;

    @ApiOperation("添加盒子类型")
    @PostMapping("/create")
    public JsonResult<String> create(@Valid CategoryCreateDto categoryCreateDto) {

        categoryService.create(categoryCreateDto);

        return new JsonResult<>();
    }

    @ApiOperation("列出盒子类型")
    @GetMapping("/list")
    public JsonResult<ListDto<CategoryDto>> list(CategorySearchDto condition, Pagination pagination) {

        condition.setTenantId(UserContext.getUser().getTenantId());

        ListDto<CategoryDto> roleDtoList = categoryService.list(condition, pagination);

        return new JsonResult<>(roleDtoList);
    }

    @ApiOperation("修改数据")
    @PostMapping("/update")
    public JsonResult<String> update(@Valid CategoryDto categoryDto) {
        categoryService.update(categoryDto);
        return new JsonResult<>();
    }

    @ApiOperation("删除盒子类型")
    @PostMapping("/delete")
    public JsonResult<String> deleteById(long id) {
        User user = UserContext.getUser();
        categoryService.deleteById(id,user.getTenantId());
        return new JsonResult<>();

    }

}
